package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public void saveWithDishing(SetmealDto setmealDto) {

        // 1. 添加套餐的信息
        this.save(setmealDto);

        // 2. 田间套餐的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {

        // 1. 配置基本数据的分页查询
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        // 2. 创建查询条件
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        this.page(pageInfo,lambdaQueryWrapper);

        // 完善查询数据
        Page<SetmealDto> dtoPage = new Page<>();
        dtoPage.setTotal(pageInfo.getTotal());

        List<Setmeal> setmeals = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList = setmeals.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);

            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(setmealDtoList);

        return dtoPage;
    }

    @Transactional
    @Override
    public void deleteWithDishing(List<Long> ids) {
        // 1. 判断选中的套餐中是否又起售的套餐
        //select count(*) from setmeal where id in(1464552668012761090...) and status = 1;
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(lambdaQueryWrapper);

        if (count > 0) {
            throw new CustomException("选中的套餐有正在起售的商品，不能直接删除！");
        }

        // 2. 先删除套餐关联的菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(setmealDishLambdaQueryWrapper);

        // 3. 删除套餐信息
        this.removeByIds(ids);
    }

    @Transactional
    @Override
    public void updateWithDishing(SetmealDto setmealDto) {
        // 1. 更新套餐的基本信息
        this.updateById(setmealDto);

        // 2. 更新套餐关联的菜品信息
        // 2.1 先删除套餐关联的菜品信息
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);

        // 2.2 添加菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }
}
